package com.manager.ctroller;

import com.example.utils.Utils;
import com.manager.domain.Person;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 */
@WebServlet(name = "LikeSelect", value = "/likeSelect")
public class LikeSelect<T> extends HttpServlet {
    QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
/*
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    //一次查几行数据
    int rows;
    //所有条数据
    long count;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        request.setCharacterEncoding("utf-8");
        String likeName = request.getParameter("likeName");
        String page = request.getParameter("page");
        System.out.println("likeName = " + likeName);
        List<Person> personList = null;


        //一次查几条数据
        rows = 5;

        //获取所有条数据
        count = (Long) countPage();
        //一共多少页

        //最后数据不足5页

        long l = count % rows == 0 ? count / rows : (count / rows) + 1;
        System.out.println(l);
        if (page == null) {
            personList = limitSelect(likeName, 1, rows);
        }

        personList = limitSelect(likeName, Integer.parseInt(page), rows);
        //==============================================================
        List<Person> people;
        //一次查几条数据
        rows = 5;

        //获取所有条数据
        count = (Long) countPage();
        //一共多少页

        //最后数据不足5页

        System.out.println(l);
        if (page == null) {
            people = limitSelect(likeName, 1, rows);
        }

        people = limitSelect(likeName, Integer.parseInt(page), rows);

        //设置返回的数据
        session.setAttribute("page", people);
        session.setAttribute("likeCountSize", l);
        session.setAttribute("count", count);
        System.out.println("countSize = " + count);
        response.sendRedirect("likeManager.jsp?page=1");

    }


    public List<Person> limitSelect(String sname, int pageIndex, int pageSize) {
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
        try {
            sname = "%" + sname + "%";
            return queryRunner.query(
                    "select * from manager.person where name like ? limit ?,?"
                    , new BeanListHandler<>(Person.class)
                    , sname, (pageIndex - 1) * 5, pageSize
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    */

    /**
     * 获取有多少条数据
     *
     * @return
     *//*

    private Object countPage() {
        try {
            return queryRunner.query(
                    "select count(*) from manager.person"
                    , new ScalarHandler()
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //new
    public List<Person> linkSelect(String sname) throws SQLException {
        sname = "%" + sname + "%";

        String sql = "select * from manager.person where name like ?";
        List<Person> query = queryRunner.query(
                sql,
                new BeanListHandler<>(Person.class)
                , sname
        );

        for (Person person : query) {
            System.out.println("遍历到的数据 = " + person);
        }
        return query;
    }
*/
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //获取要查询的值
        String sname = req.getParameter("likeName");
        System.out.println("sname = " + sname);
        List<Person> people = likeData(sname);

        HttpSession session = req.getSession();
        //设置给前台的数据
        session.setAttribute("likePage", people);
        resp.sendRedirect("likeManager.jsp");
    }

    /**
     * 计算页码数 获取最多数据条数
     *
     * @return int
     */
    public Object countPage() {
        try {
            return queryRunner.query(
                    "select count(*) from manager.person"
                    , new ScalarHandler()
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


    public List<Person> likeData(String sname) {
        //sql拼接% 进行模糊查询
        sname = "%" + sname + "%";

        // 使用PageHelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
        try {
            //将结果返回
            return queryRunner.query(
                    "select * from manager.person where name like ?"
                    , new BeanListHandler<>(Person.class)
                    , sname
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
